package com.yzg.simple.netty.hartbeat;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @author misterWei
 * @mailbox mynameisweiyan@gmail.com
 */
public class NettyHeartbeatServerHandler extends ChannelInboundHandlerAdapter{
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof IdleStateEvent) {
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;
            String mes = "";
            switch (idleStateEvent.state()){
                case WRITER_IDLE:
                   mes = "写事件超时了~~~";
                   break;
                case READER_IDLE:
                    mes = "读时间超时了~~~";
                    break;
                case ALL_IDLE:
                    mes = "读写事件超时了~~~";
                    break;
                 default:
                     mes = null;
            }
            System.out.println(mes);
            //服务器操作 如果某个事件超过了预期阈值就可以关闭了该连接资源了
            ctx.close();
        }

    }
}
